from django import template
register = template.Library()

from django.utils.html import format_html

# 商品分类名字缩进
@register.simple_tag
def suojin(res):
    # 根据路径的,来进行缩进
    num = res.count(',')-1
    res = num * '﹂＿'
    return format_html(res)

# 自定义标签,解决 购物车价格小计
@register.simple_tag
def cartmoney(n1,n2):
    res = n1*n2
    # return round(res,2)
    # return "{:.2f}".format(res)
    return '%.2f'%(res)



# 自定义分类标签
@register.simple_tag
def Showpages(count,request):

    p = int(request.GET.get('p',1))
    begin = p-4
    end = p+5
    # 判断如果当前页数小于5
    if p < 5:
        begin = 1
        end = 10
    # 判断当前页数如果大于总页数－５
    if p > (count-5):
        begin = count -9
        end = count
    # 判断如果总页数小于１０
    if count < 10:
        begin = 1
        end = count

    # 获取页面带参数的搜索条件　&type=username&keywords=111

    data = request.GET.dict()
    # 删除带有页数参数的键，加上None如果没有页数的时候也不会报错
    data.pop('p',None)
    res = ''
    # print(data)
    for k,v in data.items():
        # print(k,v)
        res += '&'+k+'='+v
    # 输出拼接后的结果
    # print(res)



    ps = ''
    # 首页
    ps += '<li><a href="?p=１'+res+'">首页</a></li>'
    # 上一页
    if p > 1:
        ps += '<li><a href="?p='+str(p-1)+res+'">«</a></li>'

    for x in range(begin,end+1):
        # 判断是否是当前页
        if p == x:
            ps += '<li class="am-active"><a href="?p='+str(x)+res+'">'+str(x)+'</a></li>'

        else:
            ps += '<li><a href="?p='+str(x)+res+'">'+str(x)+'</a></li>'
    if p < count:
    # 下一页
        # ps += '<li><a href="?p='+str(p＋1)+'">»</a></li>'
        ps += '<li><a href="?p='+str(p+1)+res+'">»</a></li>'


    # 尾页
    # ps += '<li><a href="?p='＋str(count)＋'">尾页</a></li>'
    ps += '<li><a href="?p='+str(count)+res+'">尾页</a></li>'


    return format_html(ps)